/**
 * Created by yunpeng on 2015/4/20.
 */
var Table = require('../utils/Table').Table;

var Advice = function(content, userid, createtime) {
    this.content = content;
    this.userid = userid;
    this.createtime= createtime;
    if (typeof this.saveAdvice != 'function') {
        Advice.prototype.saveAdvice = function (callback) {
            new Table('advice').insert(this, function (err, result) {
                if (err) {
                    callback(500);
                } else {
                    callback(true);
                }
            });
        }
    }
    if (typeof this.getAdviceList!='function'){
        Advice.prototype.getAdviceList = function(params,page,callback){

            var columns = ' l.id,l.content,u.phone,date_format(l.createtime,"%Y-%m-%d") as createtime ';
            var condition1 = ' and l.userid = u.id ';

            if(params[0]&&params[0]!='null'){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]&&params[1]!='null'){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]&&params[2]!='null'){//商家名称模糊查询
                condition1+=" AND u.phone LIKE '%"+params[2]+"%'";
            }

            new Table(' advice l,users u ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });

        }

    }
}
exports.Advice = Advice;